<?php

namespace app\admin\controller\users;

use app\common\controller\Backend;
use app\common\library\Auth;
use think\Db;
use think\Env;

/**
 * 会员健康档案
 *
 * @icon fa fa-user
 */
class Info extends Backend
{


    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];
    protected $relationSearch = true;

    /**
     * @var \app\admin\model\User
     */
    protected $model = null;
    protected $models = null;
    protected $categorylist = [];
    private $redis;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Info;
        $this->redis = redis(Env::get('redis.host'), Env::get('redis.port'), Env::get('redis.pwd'), Env::get('redis.db'));
    }

    /**
     * 查看
     */
    public function index()
    {

        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                //->with(["users"=>['id','level','vip']])
                ->with([
                    'users' => function ($query) {
                        $query->withField('id,level,vip');
                    }
                ])
                ->where($where)
                ->order('id', 'desc')
                ->paginate($limit);

            if (!empty($list->items())){
                foreach ($list->items() as $k=>$v){

                    $ni = Db::table('fa_vip_level')
                        ->where('id',$v->users->level)
                        ->find();

                    $card = Db::table('fa_vip_card')
                        ->where('id',$v->users->vip)
                        ->find();

                    if (empty($ni)){
                        $list->items()[$k]->users->level = '';
                    }else{
                        $list->items()[$k]->users->level = $ni['level_name'];
                    }

                    if (empty($card)){
                        $list->items()[$k]->users->vip = '';
                    }else{
                        $list->items()[$k]->users->vip = $card['name'];
                    }
                }
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }


    /**
     * 详情
     */
    public function detail($ids)
    {
        $row = $this->model->get(['id' => $ids]);

        if (!$row) {
            $this->error(__('No Results were found'));
        }


        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    /**
     * 会员等级下拉查询
     *
     */
    public function source(){
        $list = [];
        //$json = cache('source');
        //if ($json===false){
            $lists = Db::table('fa_vip_level')
                ->field('id,level_name')
                ->select();

            if (!empty($lists)){
                foreach ($lists as $k=>$v){
                    $list[$v['id']] = $v['level_name'];
                }
            }


            $json = json($list);
            //cache('source',$json);
        //}

        return $json;

    }

    /**
     * 会员等级下拉查询
     *
     */
    public function sources(){
        $list = [];
        $lists = Db::table('fa_vip_card')
            ->field('id,name')
            ->select();

        if (!empty($lists)){
            foreach ($lists as $k=>$v){
                $list[$v['id']] = $v['name'];
            }
        }


        $json = json($list);

        return $json;

    }

}
